package keter.lab.grpc.server.handler;

import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import keter.lab.grpc.client.HelloWorldClient;
import keter.lab.grpc.proto.HelloReply;
import keter.lab.grpc.proto.HelloRequest;
import keter.lab.grpc.proto.HelloServiceGrpc;

import java.io.IOException;
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Logger;

public  class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {
    private static final Logger logger = Logger.getLogger(HelloWorldClient.class.getName());
    @Override
    public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
        logger.info("invoke server sayHello method!");
      HelloReply reply = HelloReply.newBuilder()
              .setMessage("Hello " + req.getName())
              .setOld("the old!!")
              .build();
      if(false){//some error occur
          responseObserver.onError(Status.INTERNAL.withDescription("出错了!").asException());
          responseObserver.onCompleted();
      }
      responseObserver.onNext(reply);
      responseObserver.onCompleted();
    }

    @Override
    public void listHaha(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
        logger.info("invoke server listHaha method!");
      HelloReply reply = HelloReply.newBuilder()
              .setMessage("Hello " + req.getName())
              .setOld("the 1st").build();
      responseObserver.onNext(reply);
            logger.info("等待输入...");
//            Scanner scanner = new Scanner(System.in);
            String line;
            while (!"q".equals(line = new Scanner(System.in).nextLine())) {
                reply = HelloReply.newBuilder()
                        .setMessage(line)
                        .setOld(line).build();
                responseObserver.onNext(reply);
                System.out.print("请继续输入！\n");
            }
            System.out.println("The program will exit");
        responseObserver.onCompleted();
    }

    public static void main(String[] args) throws IOException {
        System.out.println("a");
        System.in.read();
        System.out.println("B");
        Queue q = new ConcurrentLinkedQueue();
        q.add(1);
        q.size();
    }

  }